package ui.widget;

import util.Position;
import util.Translator;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

public class GridScreen extends MainScreen {

	protected GridArea[][] areas;
	Position currentPos=new Position();
	protected Bitmap bmpBackground ;
	protected Translator translator;
	
	public GridScreen() {
        super( Manager.USE_ALL_WIDTH | Manager.NO_VERTICAL_SCROLL );
        init();
        setReferenceSize(Display.getWidth(), Display.getHeight());
	}
	
	public void setReferenceSize(int referenceWidth , int referenceHeight) {
		translator = new Translator(referenceWidth , referenceHeight);
	}
	
	private void init() {
        add(new HorizontalFieldManager(){
        	protected void paint(Graphics graphics) {
        		super.paint(graphics);
        		paintGridBackground(graphics);
        		for (int i = 0; i < areas.length; i++) {
        			for (int j = 0; j < areas[i].length; j++) {
        				if (areas[i][j]!=null)
        					areas[i][j].paintImage(graphics, translator);
					}
				}
        		GridArea selectedArea = getSelectedArea();
        		if (selectedArea !=null)
        			selectedArea.paintSelection(graphics, translator);
        	}
        	protected void sublayout(int maxWidth, int maxHeight) {
        		super.sublayout(maxWidth, maxHeight);
        		setExtent(Display.getWidth(), Display.getHeight());
        	}
        });
	}

	protected void paintGridBackground(Graphics graphics) {
		if (graphics.getClippingRect().width>bmpBackground.getWidth() || graphics.getClippingRect().height>bmpBackground.getHeight())  { //si el fondo es mas chico que la pantalla
			//rellena con negro
			graphics.setColor(0x000000);
			graphics.fillRect(0, 0, graphics.getClippingRect().width, graphics.getClippingRect().height);
		}
		graphics.drawBitmap(0, 0, bmpBackground.getWidth(), bmpBackground.getHeight(), bmpBackground , 0, 0);
	}
	
	private GridArea getSelectedArea() {
		return areas[currentPos.y][currentPos.x];
	}
	

	protected boolean navigationMovement(int dy, int dx, int status, int time) {
		System.out.println("nav("+dx+","+dy+")");
		System.out.print(currentPos+"->");
		Position prevPos = currentPos.copy();
		if (dx<0 && currentPos.x > 0) {
			if (currentPos.x > 0 )
				currentPos.x--;//avanza uno
			//sigue en la misma direccion si el area actual es null
			while (currentPos.x > 0 && areas[currentPos.y][currentPos.x] == null) {
				currentPos.x--;
			}
		}
		else if (dx > 0) {
			if (currentPos.x<areas[currentPos.y].length-1 )
				currentPos.x++;//avanza uno
			//sigue en la misma direccion si el area actual es null
			while (currentPos.x<areas[currentPos.y].length-1 && areas[currentPos.y][currentPos.x] == null) {
				currentPos.x++;
			}
		}
		if (dy<0 ) {
			if (currentPos.y>0)
				currentPos.y --;//avanza uno
			//sigue en la misma direccion si el area actual es null
			while (currentPos.y>0 && areas[currentPos.y][currentPos.x] == null) {
				currentPos.y --;
			}
		}
		else if (dy>0) {
			if (currentPos.y < areas.length-1 )
				currentPos.y ++; //avanza uno
			//sigue en la misma direccion si el area actual es null
			while (currentPos.y < areas.length-1 && areas[currentPos.y][currentPos.x] ==null) {
				currentPos.y ++;
			}
		}
		System.out.println(currentPos);
		if (areas[currentPos.y][currentPos.x]==null) //si quede en un null vuelve al original como si no se hubiese movido
			currentPos = prevPos;
		else if (!currentPos.equals(prevPos)) {
			XYRect newSelected = areas[currentPos.y][currentPos.x].getXYRect(translator);
			XYRect prevSelected = areas[prevPos.y][prevPos.x].getXYRect(translator);
			invalidate(prevSelected.x, prevSelected.y, prevSelected.width, prevSelected.height);
			invalidate(newSelected.x, newSelected.y, newSelected.width, newSelected.height);
		}
		return true;
	}
	
	protected boolean trackwheelClick(int status, int time) {
		GridArea selectedArea = getSelectedArea();
		if (selectedArea !=null)
			selectedArea.getRunnable().run();
		return true;
	}


}
